import { useReactive } from "ahooks";
import { useModel } from "umi";
import { useEffect } from "react";
import compress from "gql-compress";

import useGlobalSetting from "../globalSetting/useGlobalSetting";

const useAllInstitutionsToTree = () => {
    const { graphqlFetch } = useModel("global");
    const { organizationInfo } = useModel("organization.info");

    const { loadGlobalSettings } = useGlobalSetting();

    const state = useReactive({
        loading: false,
        institutionTree: { id: "root", value: { name: "-" } },
        isShowTree: false,
    });

    const loadAllInstitutionsToTree = async () => {
        state.loading = true;
        state.isShowTree = false;

        let globalSettings = await loadGlobalSettings();

        let treeData = {
            id: "root",
            oiUuid: organizationInfo.uuid,
            oiName: organizationInfo.fullName,
            type: "organization",
            maInfo: globalSettings["organization-head"],
            value: {
                name: organizationInfo.fullName,
                title: globalSettings["organization-head"]?.osName || "-",
            },
            children: [
                { id: "id-1", oiUuid: "职能部门", oiName: "职能部门", type: "category", nodeType: "分类", value: { name: "职能部门" } },
                { id: "id-2", oiUuid: "分支机构", oiName: "分支机构", type: "category", nodeType: "分类", value: { name: "分支机构" } },
                { id: "id-3", oiUuid: "项目组", oiName: "项目组", type: "category", nodeType: "分类", value: { name: "项目组" } },
                { id: "id-4", oiUuid: "专业组", oiName: "专业组", type: "category", nodeType: "分类", value: { name: "专业组" } },
                { id: "id-5", oiUuid: "兴趣组", oiName: "兴趣组", type: "category", nodeType: "分类", value: { name: "兴趣组" } },
                { id: "id-6", oiUuid: "活动组", oiName: "活动组", type: "category", nodeType: "分类", value: { name: "活动组" } },
            ],
        };

        let res = await graphqlFetch({
            query,
            variables: {},
        });
        if (res) {
            let { departments, branchs, projects, specialities, interests, activities } = res;

            departments = departments.nodes.map((item) => ({
                asInfo: item.asInfo,
                maInfo: item.maInfo,
                oiName: item.oiName,
                oiStat: item.oiStat,
                oiType: item.oiType,
                oiUuid: item.oiUuid,
                id: item.oiUuid,
                type: "department",
                nodeType: "机构",
                value: {
                    name: item.oiName,
                    title: item.maInfo.osName,
                },
                children: item.children.nodes.map((_item, i) => ({
                    id: item.oiUuid + "-member-" + i,
                    type: "department-member",
                    reUuid: _item.reUuid,
                    reType: _item.reType,
                    oiUuid: _item.oiUuid,
                    ..._item.staff,
                    ..._item.position,
                    value: {
                        title: _item.position.opName || "-",
                        name: _item.staff.osName,
                        type: "成员",
                    },
                })),
            }));

            branchs = branchs.nodes.map((item) => {
                let children = [];

                item.children.nodes.forEach((_item, i) =>
                    children.push({
                        id: item.oiUuid + "-member-" + i,
                        type: "branch-member",
                        reUuid: _item.reUuid,
                        reType: _item.reType,
                        oiUuid: _item.oiUuid,
                        ..._item.staff,
                        ..._item.position,
                        value: {
                            title: _item.position.opName || "-",
                            name: _item.staff.osName,
                            type: "成员",
                        },
                    })
                );

                item.departments.nodes.forEach((_item, i) =>
                    children.push({
                        asInfo: _item.asInfo,
                        maInfo: _item.maInfo,
                        oiName: _item.oiName,
                        oiStat: _item.oiStat,
                        oiType: _item.oiType,
                        oiUuid: _item.oiUuid,
                        id: _item.oiUuid,
                        type: "branch-department",
                        nodeType: "机构",
                        value: {
                            name: _item.oiName,
                            title: _item.maInfo.osName,
                            type: "机构",
                        },
                        children: _item.children.nodes.map((__item, i) => ({
                            id: _item.oiUuid + "-member-" + i,
                            type: "branch-department-member",
                            reUuid: __item.reUuid,
                            reType: __item.reType,
                            oiUuid: __item.oiUuid,
                            ...__item.staff,
                            ...__item.position,
                            value: {
                                title: __item.position.opName || "-",
                                name: __item.staff.osName,
                                type: "成员",
                            },
                        })),
                    })
                );

                return {
                    asInfo: item.asInfo,
                    maInfo: item.maInfo,
                    oiName: item.oiName,
                    oiStat: item.oiStat,
                    oiType: item.oiType,
                    oiUuid: item.oiUuid,
                    id: item.oiUuid,
                    type: "branch",
                    nodeType: "机构",
                    value: {
                        name: item.oiName,
                        title: item.maInfo.osName,
                        type: "机构",
                    },
                    children,
                };
            });

            projects = projects.nodes.map((item) => {
                let children = [];

                item.children.nodes.forEach((_item, i) =>
                    children.push({
                        id: item.oiUuid + "-member-" + i,
                        type: "project-member",
                        reUuid: _item.reUuid,
                        reType: _item.reType,
                        oiUuid: _item.oiUuid,
                        ..._item.member,
                        value: {
                            title: _item.member.omPosi || "-",
                            name: _item.member.omName,
                            type: "成员",
                        },
                    })
                );

                item.departments.nodes.forEach((_item, i) =>
                    children.push({
                        asInfo: _item.asInfo,
                        maInfo: _item.maInfo,
                        oiName: _item.oiName,
                        oiStat: _item.oiStat,
                        oiType: _item.oiType,
                        oiUuid: _item.oiUuid,
                        id: _item.oiUuid,
                        type: "project-department",
                        nodeType: "机构",
                        value: {
                            name: _item.oiName,
                            title: _item.maInfo.osName,
                            type: "机构",
                        },
                        children: _item.children.nodes.map((__item, i) => ({
                            id: _item.oiUuid + "-member-" + i,
                            type: "project-department-member",
                            reUuid: __item.reUuid,
                            reType: __item.reType,
                            oiUuid: __item.oiUuid,
                            ...__item.member,
                            value: {
                                title: __item.member.omPosi || "-",
                                name: __item.member.omName,
                                type: "成员",
                            },
                        })),
                    })
                );

                return {
                    asInfo: item.asInfo,
                    maInfo: item.maInfo,
                    oiName: item.oiName,
                    oiStat: item.oiStat,
                    oiType: item.oiType,
                    oiUuid: item.oiUuid,
                    id: item.oiUuid,
                    type: "project",
                    nodeType: "机构",
                    value: {
                        name: item.oiName,
                        title: item.maInfo.osName,
                        type: "机构",
                    },
                    children,
                };
            });

            specialities = specialities.nodes.map((item) => ({
                asInfo: item.asInfo,
                maInfo: item.maInfo,
                oiName: item.oiName,
                oiStat: item.oiStat,
                oiType: item.oiType,
                oiUuid: item.oiUuid,
                id: item.oiUuid,
                type: "specialitiy",
                nodeType: "机构",
                value: {
                    name: item.oiName,
                    title: item.maInfo.osName,
                    type: "机构",
                },
                children: item.children.nodes.map((_item, i) => ({
                    id: item.oiUuid + "-member-" + i,
                    type: "specialitiy-member",
                    reUuid: _item.reUuid,
                    reType: _item.reType,
                    oiUuid: _item.oiUuid,
                    ..._item.member,
                    value: {
                        title: _item.member.omPosi || "-",
                        name: _item.member.omName,
                        type: "成员",
                    },
                })),
            }));

            interests = interests.nodes.map((item) => ({
                asInfo: item.asInfo,
                maInfo: item.maInfo,
                oiName: item.oiName,
                oiStat: item.oiStat,
                oiType: item.oiType,
                oiUuid: item.oiUuid,
                id: item.oiUuid,
                type: "interest",
                nodeType: "机构",
                value: {
                    name: item.oiName,
                    title: item.maInfo.osName,
                    type: "机构",
                },
                children: item.children.nodes.map((_item, i) => ({
                    id: item.oiUuid + "-member-" + i,
                    type: "interest-member",
                    reUuid: _item.reUuid,
                    reType: _item.reType,
                    oiUuid: _item.oiUuid,
                    ..._item.member,
                    value: {
                        title: _item.member.omPosi || "-",
                        name: _item.member.omName,
                        type: "成员",
                    },
                })),
            }));

            activities = activities.nodes.map((item) => ({
                asInfo: item.asInfo,
                maInfo: item.maInfo,
                oiName: item.oiName,
                oiStat: item.oiStat,
                oiType: item.oiType,
                oiUuid: item.oiUuid,
                id: item.oiUuid,
                type: "activity",
                nodeType: "机构",
                value: {
                    name: item.oiName,
                    title: item.maInfo.osName,
                    type: "机构",
                },
                children: item.children.nodes.map((_item, i) => ({
                    id: item.oiUuid + "-member-" + i,
                    type: "activity-member",
                    reUuid: _item.reUuid,
                    reType: _item.reType,
                    oiUuid: _item.oiUuid,
                    ..._item.member,
                    value: {
                        title: _item.member.omPosi || "-",
                        name: _item.member.omName,
                        type: "成员",
                    },
                })),
            }));

            treeData.children[0].children = departments;
            treeData.children[1].children = branchs;
            treeData.children[2].children = projects;
            treeData.children[3].children = specialities;
            treeData.children[4].children = interests;
            treeData.children[5].children = activities;
        }

        state.institutionTree = treeData;
        // let institutions =
        //     res?.a?.nodes.map((item) => {
        //         let persons = item.children.nodes.map((item) => ({
        //             ...item,
        //             id: item.reUuid,
        //             type: "person",
        //         }));
        //         let departments = item.departments.nodes.map((item) => ({
        //             ...item,
        //             id: item.oiUuid,
        //             type: "department",
        //             children: item.children.nodes.map((item) => ({
        //                 ...item,
        //                 id: item.reUuid,
        //                 type: "person",
        //             })),
        //         }));
        //         return {
        //             ...item,
        //             id: item.oiUuid,
        //             type: "institution",
        //             children: persons.concat(departments),
        //         };
        //     }) || [];
        // state.institutionTree = institutions;
        state.loading = false;
        state.isShowTree = true;
        return treeData;
    };

    useEffect(() => {
        organizationInfo?.uuid && loadAllInstitutionsToTree();
    }, [organizationInfo?.uuid]);

    return { ...state, loadAllInstitutionsToTree };
};

const query = compress(`query MyQuery {
    departments: allOrganizationInstitutions(
        condition: { oiType: "职能部门", oiRoot: null },
        orderBy: PY_NAME_ASC
    ) {
        nodes {
            oiName
            asInfo
            maInfo
            oiPaid
            oiType
            oiUuid
            children: organizationInstitutionsMembersByOiUuid {
                nodes {
                    reUuid
                    reType
                    oiUuid
                    staff: organizationStaffByOsUuid {
                        osUuid
                        osName
                    }
                    position: organizationPositionByOpUuid {
                        opUuid
                        opName
                    }
                }
            }
        }
    }

    branchs: allOrganizationInstitutions(
        condition: { oiType: "分支机构", oiRoot: null }
        orderBy: PY_NAME_ASC
    ) {
        nodes {
            asInfo
            maInfo
            oiName
            oiStat
            oiType
            oiUuid
            children: organizationInstitutionsMembersByOiUuid {
                nodes {
                    reUuid
                    reType
                    oiUuid
                    staff: organizationStaffByOsUuid {
                        osUuid
                        osName
                    }
                    position: organizationPositionByOpUuid {
                        opUuid
                        opName
                    }
                }
            }
            departments: organizationInstitutionsByOiRoot {
                nodes {
                    asInfo
                    maInfo
                    oiName
                    oiStat
                    oiType
                    oiUuid
                    children: organizationInstitutionsMembersByOiUuid {
                        nodes {
                            reUuid
                            reType
                            oiUuid
                            oimRemk
                            staff: organizationStaffByOsUuid {
                                osUuid
                                osName
                            }
                            position: organizationPositionByOpUuid {
                                opUuid
                                opName
                            }
                        }
                    }
                }
            }
        }
    }

    projects: allOrganizationInstitutions(
        condition: { oiType: "项目组", oiRoot: null },
        orderBy: PY_NAME_ASC
    ) {
        nodes {
            oiName
            asInfo
            maInfo
            oiPaid
            oiType
            oiUuid
            children: organizationInstitutionsMembersByOiUuid {
                nodes {
                    reUuid
                    reType
                    oiUuid
                    oimRemk
                    member: organizationMemberByOmUuid {
                        omUuid
                        omName
                        omAvta
                        omRoge
                        omPosi
                        omEona
                        omEouu
                        omIfim
                    }
                }
            }
            departments: organizationInstitutionsByOiRoot {
                nodes {
                    asInfo
                    maInfo
                    oiName
                    oiStat
                    oiType
                    oiUuid
                    children: organizationInstitutionsMembersByOiUuid {
                        nodes {
                            reUuid
                            reType
                            oiUuid
                            oimRemk
                            member: organizationMemberByOmUuid {
                                omUuid
                                omName
                                omAvta
                                omRoge
                                omPosi
                                omEona
                                omEouu
                                omIfim
                            }
                        }
                    }
                }
            }
        }
    }

    specialities: allOrganizationInstitutions(
        condition: { oiType: "专业组", oiRoot: null },
        orderBy: PY_NAME_ASC
    ) {
        nodes {
            oiName
            asInfo
            maInfo
            oiPaid
            oiType
            oiUuid
            children: organizationInstitutionsMembersByOiUuid {
                nodes {
                    reUuid
                    reType
                    oiUuid
                    oimRemk
                    member: organizationMemberByOmUuid {
                        omUuid
                        omName
                        omAvta
                        omRoge
                        omPosi
                        omEona
                        omEouu
                        omIfim
                    }
                }
            }
        }
    }

    interests: allOrganizationInstitutions(
        condition: { oiType: "兴趣组", oiRoot: null },
        orderBy: PY_NAME_ASC
    ) {
        nodes {
            oiName
            asInfo
            maInfo
            oiPaid
            oiType
            oiUuid
            children: organizationInstitutionsMembersByOiUuid {
                nodes {
                    reUuid
                    reType
                    oiUuid
                    oimRemk
                    member: organizationMemberByOmUuid {
                        omUuid
                        omName
                        omAvta
                        omRoge
                        omPosi
                        omEona
                        omEouu
                        omIfim
                    }
                }
            }
        }
    }


    activities: allOrganizationInstitutions(
        condition: { oiType: "活动组", oiRoot: null },
        orderBy: PY_NAME_ASC
    ) {
        nodes {
            oiName
            asInfo
            maInfo
            oiPaid
            oiType
            oiUuid
            children: organizationInstitutionsMembersByOiUuid {
                nodes {
                    reUuid
                    reType
                    oiUuid
                    oimRemk
                    member: organizationMemberByOmUuid {
                        omUuid
                        omName
                        omAvta
                        omRoge
                        omPosi
                        omEona
                        omEouu
                        omIfim
                    }
                }
            }
        }
    }
}`);

export default useAllInstitutionsToTree;
